package avatar

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/avatar"
	avatarReq "github.com/flipped-aurora/gin-vue-admin/server/model/avatar/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
)

type AvatarService struct {
}

// CreateAvatar 创建Avatar记录
// Author [piexlmax](https://github.com/piexlmax)
func (avatarService *AvatarService) CreateAvatar(avatar avatar.Avatar) (err error) {
	err = global.GVA_DB.Create(&avatar).Error
	return err
}
func CreateAvatarServ(avatar avatar.Avatar) (err error) {
	err = global.GVA_DB.Create(&avatar).Error
	return err
}

// DeleteAvatar 删除Avatar记录
// Author [piexlmax](https://github.com/piexlmax)
func (avatarService *AvatarService) DeleteAvatar(avatar avatar.Avatar) (err error) {
	err = global.GVA_DB.Delete(&avatar).Error
	return err
}

// DeleteAvatarByIds 批量删除Avatar记录
// Author [piexlmax](https://github.com/piexlmax)
func (avatarService *AvatarService) DeleteAvatarByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]avatar.Avatar{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateAvatar 更新Avatar记录
// Author [piexlmax](https://github.com/piexlmax)
func (avatarService *AvatarService) UpdateAvatar(avatar avatar.Avatar) (err error) {
	err = global.GVA_DB.Save(&avatar).Error
	return err
}

// GetAvatar 根据id获取Avatar记录
// Author [piexlmax](https://github.com/piexlmax)
func (avatarService *AvatarService) GetAvatar(id uint) (avatar avatar.Avatar, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&avatar).Error
	return
}

// GetAvatarInfoList 分页获取Avatar记录
// Author [piexlmax](https://github.com/piexlmax)
func (avatarService *AvatarService) GetAvatarInfoList(info avatarReq.AvatarSearch) (list interface{}, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&avatar.Avatar{})
	var avatars []avatar.Avatar
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.Avatar_user != nil {
		db = db.Where("avatar_user = ?", info.Avatar_user)
	}
	if info.Avatar_position != "" {
		db = db.Where("avatar_position = ?", info.Avatar_position)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Limit(limit).Offset(offset).Find(&avatars).Error
	return avatars, total, err
}
